//
//  XPPrivacyViewController.swift
//  PPKit
//
//  Created by xiaopin on 2020/7/24.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//

import UIKit

/// 隐私政策
class XPPrivacyViewController: XPBaseViewController,UITextViewDelegate {

    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var labTitle: UILabel!
    @IBOutlet weak var labContent: HyperlinkLabel!
    @IBOutlet weak var btnAgree: UIButton!
    @IBOutlet weak var btnUnagree: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.contentView.cornerRadius = 12
        self.btnAgree.cornerRadius = 4
        
        labTitle.text = "Alert_ProtocolTitle".localized
            
        var text = "Alert_iOS_Protocol".localized
        if text.contains(find: "[n]") && text.contains(find: "[m]"){
            let termsOfUseStr = "ME_Login_TermsOfuse".localized
            let privacyStr = "ME_Login_Privacy".localized
            text = text.replacingOccurrences(of: "[n]", with: "%0$@").replacingOccurrences(of: "[m]", with: "%1$@")
        
            let attributedString = NSMutableAttributedString(string: text)
            let replacements = [(termsOfUseStr, URL(string: "https://link1")!),
                                (privacyStr, URL(string: "https://link2")!)]
            
            replacements.enumerated().forEach { index, value in
                let attributes: [NSAttributedString.Key: Any] = [.hyperlink: value.1]
                let urlAttributedString = NSAttributedString(string: value.0, attributes: attributes)
                let range = (attributedString.string as NSString).range(of: "%\(index)$@")
                attributedString.replaceCharacters(in: range, with: urlAttributedString)
            }
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .left
            paragraphStyle.lineSpacing = 5
            attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
           
            //let labContent = HyperlinkLabel()
            labContent.hyperlinkAttributes = [.foregroundColor:pp_color(.C159F48)]
            labContent.attributedText = attributedString
            labContent.translatesAutoresizingMaskIntoConstraints = false
            labContent.didTapOnURL = { [weak self] (url) in
                if url.absoluteString == "https://link1" {
                    let vc = XPBaseWebViewController()
                    vc.title = "ME_Login_TermsOfuse".localized
                    vc.webUrl = kContentAgreementUrl(.termsofuse)
                    self?.push(vc)
                }else if url.absoluteString == "https://link2" {
                    let vc = XPBaseWebViewController()
                    vc.title = "ME_Login_Privacy".localized
                    vc.webUrl = kContentAgreementUrl(.priavcyPolicy)
                    self?.push(vc)
                }
            }
        }
        btnAgree.setTitle("MT_Agree".localized)
        btnUnagree.setTitle("MT_UnagreeExit".localized)
    
        pp_alert_show(self.contentView)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.hideNavigationBar(animated)
    
    }

    //MARK: 自定义方法
    
    func exitApplication(){
        let app = UIApplication.shared.delegate
        let window = app?.window
        
        UIView.animate(withDuration: 0.4, animations: {
            window??.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
        }) { (b) in
            exit(0)
        }
    }

    //MARK: 事件通知
    @IBAction func agreeAction(_ sender: Any) {
//        XPApplication.shared.isAgreedPrivacy = true
//        pp_alert_hide(self.contentView)
//        self.dismiss(animated: true, completion: nil)
//
//        NotificationCenter.default.post(name: .xpfPrivacyAgreed, object: nil)
    }
    
    @IBAction func unagreeAction(_ sender: Any) {
        pp_alert_hide(self.contentView)
        self.dismiss(animated: true, completion: nil)
        pp_dispatchAfter(after: 1) {
            self.exitApplication()
        }
    }
    
    //MARK: 委托代理
}
